using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpriteClasses
{
    public class Sprite
    {
        protected Vector2 velocity, originalVelocity, position, spriteOrigin;
        protected float moveSpeed;
        public float MoveSpeed { get { return moveSpeed; } set { moveSpeed = value; } }
        public bool Active { get; set; }
        public Vector2 Velocity { get { return velocity; } set { velocity = value; } }
        public Vector2 OriginalVelocity { get { return originalVelocity; } set { originalVelocity = value; } }
        public Vector2 Position { get{return position;} set { position = value; } }
        public float Rotation { get; set; }
        public float RotationSpeed { get; set; }
        public virtual Rectangle CollisionRectangle
        {
            get
            {
                return new Rectangle((int)(position.X - SpriteOrigin.X * Scale), (int)(position.Y - SpriteOrigin.Y * Scale),
                    Convert.ToInt32(TextureImage.Width * Scale), Convert.ToInt32(TextureImage.Height * Scale));
            }
        }
        public float Scale { get; set; }
        public SpriteEffects SpriteEffect { get; set; }
        public Vector2 SpriteOrigin { get { return spriteOrigin; } set { spriteOrigin = value; } }
        public Texture2D TextureImage { get; set; }
        

        public Sprite(Texture2D textureImage,Vector2 position, Vector2 velocity, float rotationSpeed, float scale, SpriteEffects spriteEffect,float moveSpeed)
        {
            this.moveSpeed = moveSpeed;
            TextureImage = textureImage;
            Position = position;
            Velocity = velocity;
            RotationSpeed = rotationSpeed;

            SpriteOrigin = new Vector2(textureImage.Width / 2, textureImage.Height / 2); ;
            Scale = scale;
            SpriteEffect = spriteEffect;
            OriginalVelocity = velocity;
            Active = true;
            
        }
        public Sprite(Texture2D textureImage, Vector2 position, Vector2 velocity,Vector2 origin, float rotationSpeed, float scale, SpriteEffects spriteEffect, float moveSpeeds)
        {
            moveSpeed = moveSpeeds;
            TextureImage = textureImage;
            Position = position;
            Velocity = velocity;
            RotationSpeed = rotationSpeed;
            SpriteOrigin = origin;
            Scale = scale;
            SpriteEffect = spriteEffect;
            OriginalVelocity = velocity;
            Active = true;

        }
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (Active)
            {
                spriteBatch.Draw(TextureImage, Position, null, Color.White, Rotation, SpriteOrigin, Scale, SpriteEffect, 0);
            }
        }
        public virtual void Update(GameTime gameTime)
        {
            if (Active)
            {
                float timelapse = (float)gameTime.ElapsedGameTime.TotalSeconds;
            
                position += Velocity * timelapse;
                Rotation += RotationSpeed;
            }
        }
        public virtual void Update(GameTime gameTime, GraphicsDevice Device)
        {
            if (Active)
            {
                Update(gameTime);
                if (position.Y > Device.Viewport.Height - (SpriteOrigin.Y * Scale) || position.Y < 0 + (SpriteOrigin.Y * Scale))
                {
                    position.Y = Device.Viewport.Height - (SpriteOrigin.Y * Scale);
                    velocity *= 0.75f;
                }
                else if (position.Y < 0 + (SpriteOrigin.Y * Scale))
                {
                    position.Y = 0 + (SpriteOrigin.Y * Scale);
                    velocity *= 0.75f;
                }

                if (position.X > Device.Viewport.Width - (SpriteOrigin.X * Scale) || position.X < 0 + (SpriteOrigin.X * Scale))
                {
                    position.X = Device.Viewport.Width - (SpriteOrigin.X * Scale);
                    velocity *= 0.75f;
                }
                else if (position.X < 0 + (SpriteOrigin.X * Scale))
                {
                    position.X = 0 + (SpriteOrigin.X * Scale);
                    velocity *= 0.75f;
                }
            }
        }
        public bool CollisionMouse(int x, int y)
        {
            Point temp = new Point(x,y);
            return CollisionRectangle.Contains(temp);
        }
        public bool CollisionSprite(Sprite sprite)
        {
            return CollisionRectangle.Intersects(sprite.CollisionRectangle);
        }
        public void Up()
        {
            velocity.Y -= moveSpeed;
        }
        public void Down()
        {
            velocity.Y += moveSpeed;
        }
        public void Left()
        {
            velocity.X -= moveSpeed;
        }
        public void Right()
        {
            velocity.X += moveSpeed;
        }
        public virtual void Idle()
        {
            velocity *= 0.75f; // to slow down
            /*if (velocity.X < 0.001 && velocity.Y < 0.001 || velocity.X > -0.001 && velocity.Y > -0.001)
            {
                velocity = Vector2.Zero;
            }*/
        }
        public virtual bool IsOffScreen(GraphicsDevice Device)
        {
            if (position.X < -TextureImage.Width * Scale ||
                position.X > Device.Viewport.Width ||
                position.Y < -TextureImage.Height * Scale ||
                position.Y > Device.Viewport.Height)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
